<?

class {$name}_base_peer
{
	const TABLE = '{$name}s';
	const PRIMARY = 'id';
	const CONNECTION = 'mysql';

	public static function get( $pkey )
	{
		if ( !$data = db_mysql::cache(self::TABLE . $pkey) )
			db_mysql::cache(self::TABLE . $pkey, $data = self::get_by(self::PRIMARY, $pkey));
		
		return $data;
	}

	public static function get_by($name, $value)
	{
		return db_mysql::get_row(array($name => $value), self::TABLE, self::CONNECTION);
	}

	public static function insert( array $data )
	{
		$s = db_mysql::insert( $data, self::TABLE, false, self::CONNECTION );
		return db_mysql::last_id(self::CONNECTION);
	}

	public static function save( $pkey, array $data )
	{
		db_mysql::update( $data, array(self::PRIMARY => $pkey), self::TABLE, self::CONNECTION );
		db_mysql::cache(self::TABLE . $pkey, false);
	}

	public static function delete( $pkey )
	{
		db_mysql::delete( array(self::PRIMARY => $pkey), self::TABLE, true, self::CONNECTION );
		db_mysql::cache(self::TABLE . $pkey, false);
	}

	public static function truncate()
	{
		return db_mysql::query('TRUNCATE ' . self::TABLE, array(), self::CONNECTION);
	}
}